package com.atguigu.mapreduce.demo.writable;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/18 19:33
 * @description:
 */
public class PCounterReduce extends Reducer<Text, PhoneCounterPojo, Text, PhoneCounterPojo> {

    private PhoneCounterPojo outV = new PhoneCounterPojo();


    /**
     *
     * @param key
     * @param values
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<PhoneCounterPojo> values, Context context) throws IOException, InterruptedException {

        long upFlow = 0;
        long downFlow = 0;
        for (PhoneCounterPojo value : values) {
            upFlow += value.getUpFlow();
            downFlow += value.getDownFlow();
        }

        outV.setUpFlow(upFlow);
        outV.setDownFlow(downFlow);
        outV.setSumFlow();

        context.write(key, outV);
    }
}

